package c.c.b.a.b.a.h;

import c.c.b.a.a.e;
import c.c.b.a.a.h;
import c.c.b.a.a.j;
import c.c.b.a.a.o;
import c.c.b.a.a.r;
import c.c.b.a.a.w;
import c.c.b.a.b.a.b;
import c.c.b.a.b.a.c.g;
import c.c.b.a.b.a.e.d;
import c.c.b.a.b.a.h.q.c;
import c.c.b.a.b.a0;
import c.c.b.a.b.c0;
import c.c.b.a.b.v;
import c.c.b.a.b.z;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/* compiled from: Http2Codec */
public final class f implements d {
    public static final h e = h.d("connection");
    public static final h f = h.d("host");
    public static final h g = h.d("keep-alive");
    public static final h h = h.d("proxy-connection");
    public static final h i = h.d("transfer-encoding");
    public static final h j = h.d("te");
    public static final h k = h.d("encoding");
    public static final h l = h.d("upgrade");
    public static final List<h> m;
    public static final List<h> n;
    public final c.c.b.a.b.x.a a;
    public final g b;
    public final g c;
    public q d;

    /* compiled from: Http2Codec */
    public class a extends j {
        public boolean b = false;
        public long c = 0;

        public a(w wVar) {
            super(wVar);
        }

        public long b(e eVar, long j) throws IOException {
            try {
                long b = this.a.b(eVar, j);
                if (b > 0) {
                    this.c += b;
                }
                return b;
            } catch (IOException e) {
                k(e);
                throw e;
            }
        }

        public void close() throws IOException {
            this.a.close();
            k(null);
        }

        public final void k(IOException iOException) {
            if (!this.b) {
                this.b = true;
                f fVar = f.this;
                fVar.b.f(false, fVar, this.c, iOException);
            }
        }
    }

    static {
        m = c.c.b.a.b.a.e.l(r0, r1, r2, r3, r5, r4, r6, r7, c.f, c.g, c.h, c.i);
        n = c.c.b.a.b.a.e.l(r0, r1, r2, r3, r5, r4, r6, r7);
    }

    public f(z zVar, c.c.b.a.b.x.a aVar, g gVar, g gVar2) {
        this.a = aVar;
        this.b = gVar;
        this.c = gVar2;
    }

    public void a(c0 c0Var) throws IOException {
        if (this.d == null) {
            q qVar;
            Object obj = null;
            int i = c0Var.d != null ? 1 : 0;
            v vVar = c0Var.c;
            ArrayList arrayList = new ArrayList(vVar.a() + 4);
            arrayList.add(new c(c.f, c0Var.b));
            arrayList.add(new c(c.g, b.i.b.f.i(c0Var.a)));
            String c = c0Var.c.c("Host");
            if (c != null) {
                arrayList.add(new c(c.i, c));
            }
            arrayList.add(new c(c.h, c0Var.a.a));
            int a = vVar.a();
            for (int i2 = 0; i2 < a; i2++) {
                h d = h.d(vVar.b(i2).toLowerCase(Locale.US));
                if (!m.contains(d)) {
                    arrayList.add(new c(d, vVar.d(i2)));
                }
            }
            g gVar = this.c;
            boolean z = i ^ 1;
            synchronized (gVar.p) {
                int i3;
                synchronized (gVar) {
                    if (gVar.g) {
                        throw new a();
                    }
                    i3 = gVar.f;
                    gVar.f = i3 + 2;
                    qVar = new q(i3, gVar, z, false, arrayList);
                    if (i == 0 || gVar.k == 0 || qVar.b == 0) {
                        obj = 1;
                    }
                    if (qVar.b()) {
                        gVar.c.put(Integer.valueOf(i3), qVar);
                    }
                }
                r rVar = gVar.p;
                synchronized (rVar) {
                    if (rVar.e) {
                        throw new IOException("closed");
                    }
                    rVar.t(z, i3, arrayList);
                }
            }
            if (obj != null) {
                gVar.p.v();
            }
            this.d = qVar;
            c cVar = qVar.j;
            long j = (long) ((c.c.b.a.b.a.e.g) this.a).j;
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            cVar.b(j, timeUnit);
            this.d.k.b((long) ((c.c.b.a.b.a.e.g) this.a).k, timeUnit);
        }
    }

    public void b() throws IOException {
        ((c.c.b.a.b.a.h.q.a) this.d.e()).close();
    }

    public c.c.b.a.a.v c(c0 c0Var, long j) {
        return this.d.e();
    }

    public c.c.b.a.b.e b(c.c.b.a.b.c cVar) throws IOException {
        Objects.requireNonNull(this.b.f);
        String c = cVar.f.c("Content-Type");
        if (c == null) {
            c = null;
        }
        long b = c.c.b.a.b.a.e.f.b(cVar);
        a aVar = new a(this.d.h);
        Logger logger = o.a;
        return new c.c.b.a.b.a.e.h(c, b, new r(aVar));
    }

    public void a() throws IOException {
        this.c.p.v();
    }

    public c.c.b.a.b.c.a a(boolean z) throws IOException {
        List list;
        q qVar = this.d;
        synchronized (qVar) {
            if (qVar.c()) {
                qVar.j.h();
                while (qVar.f == null && qVar.l == null) {
                    try {
                        qVar.i();
                    } finally {
                        qVar.j.n();
                    }
                }
                list = qVar.f;
                if (list != null) {
                    qVar.f = null;
                } else {
                    throw new w(qVar.l);
                }
            }
            throw new IllegalStateException("servers cannot read response headers");
        }
        c.c.b.a.b.v.a aVar = new c.c.b.a.b.v.a();
        int size = list.size();
        c.c.b.a.b.a.e.j jVar = null;
        for (int i = 0; i < size; i++) {
            c cVar = (c) list.get(i);
            if (cVar != null) {
                h hVar = cVar.a;
                String j = cVar.b.j();
                if (hVar.equals(c.e)) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append("HTTP/1.1 ");
                    stringBuilder.append(j);
                    jVar = c.c.b.a.b.a.e.j.a(stringBuilder.toString());
                } else if (!n.contains(hVar)) {
                    b.a.c(aVar, hVar.j(), j);
                }
            } else if (jVar != null && jVar.b == 100) {
                aVar = new c.c.b.a.b.v.a();
                jVar = null;
            }
        }
        if (jVar != null) {
            c.c.b.a.b.c.a aVar2 = new c.c.b.a.b.c.a();
            aVar2.b = a0.HTTP_2;
            aVar2.c = jVar.b;
            aVar2.d = jVar.c;
            List list2 = aVar.a;
            String[] strArr = (String[]) list2.toArray(new String[list2.size()]);
            c.c.b.a.b.v.a aVar3 = new c.c.b.a.b.v.a();
            Collections.addAll(aVar3.a, strArr);
            aVar2.f = aVar3;
            if (z) {
                Objects.requireNonNull((c.c.b.a.b.z.a) b.a);
                if (aVar2.c == 100) {
                    return null;
                }
            }
            return aVar2;
        }
        throw new ProtocolException("Expected ':status' header not present");
    }
}
